define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    if(Config.accountpay == '' || Config.accountpay == null){
        Config.accountpay = 0;
    }
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'feditionaccounturl/index/?accountpay=' + Config.accountpay,
                    add_url: 'feditionaccounturl/add',
                    edit_url: 'feditionaccounturl/edit',
                    del_url: 'feditionaccounturl/del',
                    multi_url: 'feditionaccounturl/multi',
                    import_url: 'feditionaccounturl/import',
                    table: 'fedition_accounturl',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        //{field: 'id', title: __('ID'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'url', title: __('默认域名'), operate: 'LIKE'},
                        // {field: 'url2', title: __('二级域名'), operate: 'LIKE'},
                        {field: 'info', title: __('Info'), formatter:Table.api.formatter.flag, operate: 'LIKE',custom:{'Shoplaza':'info','shopify':'warning'}},
                        // {field: 'tag', title: __('标签'), formatter:Table.api.formatter.flag, operate: 'LIKE',custom:{'Shoplaza':'info','shopify':'warning'}},

                        //{field: 'tag', title: __('标签'), operate: 'LIKE', formatter:Table.api.formatter.flag,
                        //    cellStyle : function(value, row, index, field){
                        //        return {
                        //            css: {
                        //                "white-space": "nowrap",
                        //                "text-overflow": "ellipsis",
                        //                "overflow": "hidden",
                        //                "max-width":"300px"
                        //            }
                        //        };
                        //    }},

                        // {field: 'shopifyapikey', title: __('Shopifyapikey'), operate: 'LIKE'},
                        // {field: 'shopifysecretapikey', title: __('Shopifysecretapikey'), operate: 'LIKE'},
                        // {field: 'shopifyname', title: __('Shopifyname'), operate: 'LIKE'},
                        // {field: 'admin_id', title: __('Admin_id'),visible: false,addClass: "selectpage", extend: "data-source='auth/admin/index' data-field='nickname'"},
                        {field: 'admin_name', title: __('Admin_id'), operate: false},
                        //{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status_shiyong', title: ('使用状态'), searchList: {"normal":__('使用中'),"hidden":__('未使用')},defaultValue:"normal", formatter: Table.api.formatter.status},
                        {field: 'status', title: ('状态'), searchList: {"normal":__('Normal'),"hidden":__('Hidden'),"unavailable":__('已报警下线')},defaultValue:"normal", formatter: Table.api.formatter.status},
                        //{field: 'iscustom', title: __('接入shopify'), searchList: {"1":__('接入'),"0":__('暂无')}, formatter: Table.api.formatter.status,custom:{'1': 'success','0':'warning'}},
                        //{field: 'iscustom_shoplaza', title: __('接入shoplaza'), searchList: {"1":__('接入'),"0":__('暂无')}, formatter: Table.api.formatter.status,custom:{'1': 'success','0':'warning'}},
                        {field: 'ordershoplaza_time', title: __('最新同步时间'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'updateordertimec', title: __('报警时间'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'ordershoplazacount', title: __('已付款 | 已发货 | 已退款 | 部分退款'), operate:false,formatter: Controller.api.formatter.ordershoplazacount},
                        // {
                        //     name: 'detail',
                        //     text: __('店铺主题'),
                        //     title: __('查看主题'),
                        //     classname: 'btn btn-sm btn-info btn-dialog',
                        //     icon: 'fa fa-leaf',
                        //     url: 'transferthemes/index?accounturl={ids}',
                        //     //状态正常 且没有绑定卡的时候可以申请绑卡
                        //     visible:function (row) {
                        //         if(row.ispcard == 0 && row.status == 'normal' && row.payment_method == '自动支付'){
                        //             return true;
                        //         }else{
                        //             return false;
                        //         }
                        //     }
                        // },
                        //{
                        //    field: 'buttons',
                        //    width: "120px",
                        //    title: __('转移记录'),
                        //    table: table,
                        //    events: Table.api.events.operate,
                        //    operate: false,
                        //    buttons: [
                        //        {
                        //            name: 'detail',
                        //            text: __('转移记录'),
                        //            title: __('转移记录'),
                        //            classname: 'btn btn-sm btn-success btn-dialog',
                        //            icon: 'fa fa-magic',
                        //            url: 'feditionlog/index/?type=8&urlid={ids}'
                        //        }
                        //    ],
                        //    formatter: Table.api.formatter.buttons
                        //},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                $("#zcountnum").text(data.Statistics.zcountnum);
                $("#shopifysum1").text(data.Statistics.shopifysum1);
                $("#shopifysum2").text(data.Statistics.shopifysum2);
                $("#shoplaza1").text(data.Statistics.shoplaza1);
                $("#shoplaza2").text(data.Statistics.shoplaza2);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            // 监听下拉列表改变的事件
            $("#c-iscustom").bind("change",function () {
                if (this.value== 1){
                    $('#shopifyshow').show();
                }else{
                    $('#shopifyshow').hide();
                }
            });
            // 监听下拉列表改变的事件
            $("#c-iscustom_shoplaza").bind("change",function () {
                if (this.value== 1){
                    $('#shoplazashow').show();
                }else{
                    $('#shoplazashow').hide();
                }
            });
            Controller.api.bindevent();
        },
        edit: function () {
            var iscustom = $("#c-iscustom").find("option:selected").val();
            shopifyshow(iscustom);
            // 监听下拉列表改变的事件
            $("#c-iscustom").bind("change",function () {
                shopifyshow(this.value);
            });
            function shopifyshow(value) {
                if (value== 1){
                    $('#shopifyshow').show();
                }else{
                    $('#shopifyshow').hide();
                }
            }

            var iscustom = $("#c-iscustom_shoplaza").find("option:selected").val();
            shoplazashow(iscustom);
            // 监听下拉列表改变的事件
            $("#c-iscustom_shoplaza").bind("change",function () {
                shoplazashow(this.value);
            });
            function shoplazashow(value) {
                if (value== 1){
                    $('#shoplazashow').show();
                }else{
                    $('#shoplazashow').hide();
                }
            }
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                require(['selectpage'], function (SelectPage) {
                    var _displayResults = $.fn.selectPage.Constructor.prototype.displayResults;
                    $.fn.selectPage.Constructor.prototype.displayResults = function (self, json, is_query) {
                        _displayResults.call(this, self, json, is_query);
                        //这里判断是否为分类列表框，也可移除此判断
                        if (self.elem.hidden.is("#c-tag")) {
                            $(self.elem.results[0]).append("<p class='text-center' style='margin:5px;'><a class='btn btn-xs btn-info btn-dialog' title='新增标签' data-window='top' href='accounturllabel/add'>新增标签</a></p><p style='margin:5px;'>（新增标签后需要取消下拉框，重新选择）</p>");
                        }
                    };
                });
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                ordershoplazacount: function (value, row, index) {
                    console.log('value',value);
                    //这里手动构造URL
                    var url = "feditionordershoplaza/index?custom_status=1&accounturl=" + row.id;
                    var url1 = "feditionordershoplaza/index?custom_status=2&accounturl=" + row.id;
                    var url2 = "feditionordershoplaza/index?custom_status=3&accounturl=" + row.id;
                    var url3 = "feditionordershoplaza/index?custom_status=4&accounturl=" + row.id;

                    //方式一,直接返回class带有addtabsit的链接,这可以方便自定义显示内容
                    return '<a href="' + url + '" class="label addtabsit" style="color:#d43f3a;margin-right: 10px;" title="已付款'+value[0]+'单">'+value[0]+'</a>' +
    '<a href="' + url1 + '" class="label addtabsit" style="color:#d43f3a;margin-right: 10px;" title="已发货'+value[1]+'单">'+value[1]+'</a>' +
    '<a href="' + url2 + '" class="label addtabsit" style="color:#d43f3a;" title="已退款'+value[2]+'单">'+value[2]+'</a>'+'<a href="' + url3 + '" class="label addtabsit" style="color:#d43f3a;" title="已部分退款'+value[3]+'单">'+value[3]+'</a>';

                    //方式二,直接调用Table.api.formatter.addtabs
                    //this.url = url;
                    //return Table.api.formatter.addtabs.call(this, value, row, index);
                }
            }
        }
    };
    return Controller;
});